package cn.iocoder.yudao.module.erp.controller.admin.contract;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contract.vo.ContractPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contract.vo.ContractRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.contract.vo.ContractSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contract.ContractDO;
import cn.iocoder.yudao.module.erp.service.contract.ContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 合同信息")
@RestController
@RequestMapping("/erp/contract")
@Validated
public class ContractController {

    @Resource
    private ContractService contractService;


    @PostMapping("/create")
    @Operation(summary = "创建合同信息")
    @PreAuthorize("@ss.hasPermission('erp:contract:create')")
    public CommonResult<Long> createContract(@Valid @RequestBody ContractSaveReqVO createReqVO) {
        return success(contractService.createContract(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同信息")
    @PreAuthorize("@ss.hasPermission('erp:contract:update')")
    public CommonResult<Boolean> updateContract(@Valid @RequestBody ContractSaveReqVO updateReqVO) {
        contractService.updateContract(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:contract:delete')")
    public CommonResult<Boolean> deleteContract(@RequestParam("id") Long id) {
        contractService.deleteContract(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除合同信息")
                @PreAuthorize("@ss.hasPermission('erp:contract:delete')")
    public CommonResult<Boolean> deleteContractList(@RequestParam("ids") List<Long> ids) {
        contractService.deleteContractListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:contract:query')")
    public CommonResult<ContractRespVO> getContract(@RequestParam("id") Long id) {
        return success(contractService.getContract(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同信息分页")
    @PreAuthorize("@ss.hasPermission('erp:contract:query')")
    public CommonResult<PageResult<ContractRespVO>> getContractPage(@Valid ContractPageReqVO pageReqVO) {
        PageResult<ContractDO> pageResult = contractService.getContractPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同信息 Excel")
    //@PreAuthorize("@ss.hasPermission('erp:contract:export')")
    @PermitAll
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractExcel(@Valid ContractPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractRespVO> list = BeanUtils.toBean(contractService.getContractPage(pageReqVO).getList(), ContractRespVO.class);
        // 导出 Excel
        ExcelUtils.appWrite(response, "合同信息.xlsx", "数据", ContractRespVO.class,list);
    }

}